/* D:\ICADDEV\PRJ\ICAD\MTEXTTEXTTAB.CPP
 * Copyright (C) 1997-1999 DIMINE Co.LTD. All rights reserved.
 *
 * $Revision: 1.1.1.1 $ $Date: 2010/03/03 02:57:12 $
 *
 * Abstract
 *
 * <TODO : Add Abstract here>
 *
 */

// MTextTextTab.cpp : implementation file
//
#include "stdafx.h"
#include "dmlib.h"
#include "resource.h"
#include "MTextTextTab.h"
#include "MTextPropSheet.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// MTextTextTab property page

IMPLEMENT_DYNCREATE(MTextTextTab, CBCGPPropertyPage)

MTextTextTab::MTextTextTab() : CBCGPPropertyPage(MTextTextTab::IDD)
	{
	//{{AFX_DATA_INIT(MTextTextTab)
	//}}AFX_DATA_INIT
	}

MTextTextTab::~MTextTextTab()
	{
	}

void MTextTextTab::DoDataExchange(CDataExchange* pDX)
	{
	CBCGPPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(MTextTextTab)
	DDX_Control(pDX, IDC_FONT, m_font);
	DDX_Control(pDX, IDC_BOLD, m_bold);
	DDX_Control(pDX, IDC_UNDO, m_undo);
	DDX_Control(pDX, IDC_UNDERLINE, m_underline);
	DDX_Control(pDX, IDC_ITALIC, m_italic);
	DDX_Control(pDX, IDC_FONTSIZE, m_fontsize);
	//}}AFX_DATA_MAP
	}


BEGIN_MESSAGE_MAP(MTextTextTab, CBCGPPropertyPage)
	//{{AFX_MSG_MAP(MTextTextTab)
	ON_BN_CLICKED(IDC_BOLD, OnBold)
	ON_BN_CLICKED(IDC_ITALIC, OnItalic)
	ON_BN_CLICKED(IDC_UNDERLINE, OnUnderline)
	ON_CBN_SELENDOK(IDC_FONT, OnSelendokFont)
	ON_BN_CLICKED(IDC_UNDO, OnUndo)
	ON_CBN_KILLFOCUS(IDC_FONTSIZE, OnKillfocusFontsize)
	ON_CBN_SELENDOK(IDC_FONTSIZE, OnSelendokFontsize)
	ON_CBN_SETFOCUS(IDC_FONTSIZE, OnSetfocusFontsize)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// MTextTextTab message handlers


BOOL
MTextTextTab::OnInitDialog()
	{
	CBCGPPropertyPage::OnInitDialog();

	m_bold.SetIcon(AfxGetApp()->LoadIcon( IDI_TEXT_BOLD));
	m_italic.SetIcon(AfxGetApp()->LoadIcon( IDI_TEXT_ITALIC));
	m_underline.SetIcon(AfxGetApp()->LoadIcon( IDI_TEXT_UNDERLINE));
	m_undo.SetIcon(AfxGetApp()->LoadIcon( IDI_TEXT_UNDO));
	m_undo.EnableWindow(FALSE);

	m_font.FillFontComboBox( GET_FONT_SHX | GET_FONT_TTF);
	m_font.SetCurrentFont( "Arial");	

	MTextPropSheet* pPropSheet=(MTextPropSheet*) GetParent();
	m_fontsize.SetWindowText(pPropSheet->m_fsize);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
	}

void MTextTextTab::OnBold()
	{
	CRichEditCtrl* pText;
	pText = (CRichEditCtrl*) GetParent()->GetDlgItem(IDC_RICHEDIT);
	pText->SetFocus();
	CHARFORMAT cf;
	pText->GetSelectionCharFormat(cf);
	if (!(cf.dwMask & CFM_BOLD) || !(cf.dwEffects & CFE_BOLD))
		cf.dwEffects = CFE_BOLD;
	else
		cf.dwEffects = 0;
	cf.dwMask = CFM_BOLD;
	//////Fix by Qin
	cf.yHeight=250;

	pText->SetSelectionCharFormat(cf);
	((MTextPropSheet*) GetParent())->UpdateChar();
	}

void MTextTextTab::OnItalic()
	{
	CRichEditCtrl* pText;
	pText = (CRichEditCtrl*) GetParent()->GetDlgItem(IDC_RICHEDIT);
	pText->SetFocus();
	CHARFORMAT cf;
	pText->GetSelectionCharFormat(cf);
	if (!(cf.dwMask & CFM_ITALIC) || !(cf.dwEffects & CFE_ITALIC))
		cf.dwEffects = CFE_ITALIC;
	else
		cf.dwEffects = 0;
	cf.dwMask = CFM_ITALIC;
	//////Fix by Qin
	cf.yHeight=250;
	pText->SetSelectionCharFormat(cf);
	((MTextPropSheet*) GetParent())->UpdateChar();
	}

void MTextTextTab::OnUnderline()
	{
	CRichEditCtrl* pText;
	pText = (CRichEditCtrl*) GetParent()->GetDlgItem(IDC_RICHEDIT);
	pText->SetFocus();
	CHARFORMAT cf;
	pText->GetSelectionCharFormat(cf);
	if (!(cf.dwMask & CFM_UNDERLINE) || !(cf.dwEffects & CFE_UNDERLINE))
		cf.dwEffects = CFE_UNDERLINE;
	else
		cf.dwEffects = 0;
	cf.dwMask = CFM_UNDERLINE;
	//////Fix by Qin
	cf.yHeight=250;
	pText->SetSelectionCharFormat(cf);
	((MTextPropSheet*) GetParent())->UpdateChar();
	}

CFont& MTextTextTab::SelectedFont()
	{
	FontItem	*selectedFont = m_font.GetFontItem();
	LPCTSTR		selectedName = selectedFont->fontName;

	if ( selectedFont->fontType != TRUETYPE_FONTTYPE )
		m_font.SetCurrentFont("Arial");

	editFont.Detach();
	editFont.CreatePointFont( 100, selectedFont->fontName);
	return editFont;
	}

void MTextTextTab::OnSelendokFont()
	{
	CRichEditCtrl* pText = (CRichEditCtrl*) GetParent()->GetDlgItem(IDC_RICHEDIT);
	CHARFORMAT cf;
	pText->GetSelectionCharFormat(cf);
	cf.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC;
	CString cs;
	m_font.GetLBText(m_font.GetCurSel(), cs);
	::lstrcpy (cf.szFaceName, cs);
	if (((CString) m_font.GetFontItem()->fontName).Right(4) == ".shx" ||
		((CString) m_font.GetFontItem()->fontName).Right(4) == ".SHX" ||
		((CString) m_font.GetFontItem()->fontName).Right(4) == ".shp" ||
 		((CString) m_font.GetFontItem()->fontName).Right(4) == ".SHP")
		{
		if (cf.dwEffects & CFE_BOLD)
			{
			cf.dwEffects ^= CFE_BOLD;
			m_bold.SetCheck(false);
			}
		if (cf.dwEffects & CFE_ITALIC)
			{
			cf.dwEffects ^= CFE_ITALIC;
			m_italic.SetCheck(false);
			}
		}
	//////Fix by Qin
	cf.yHeight=250;
	pText->SetSelectionCharFormat(cf);
	pText->SetFocus();

	if (((CString) m_font.GetFontItem()->fontName).Right(4) == ".shx" ||
		((CString) m_font.GetFontItem()->fontName).Right(4) == ".SHX" ||
		((CString) m_font.GetFontItem()->fontName).Right(4) == ".shp" ||
		((CString) m_font.GetFontItem()->fontName).Right(4) == ".SHP")
		{
		if (m_bold.GetCheck() == false)
			m_bold.EnableWindow(FALSE);
		if (m_italic.GetCheck() == false)
			m_italic.EnableWindow(FALSE);
		}
	else
		{
		m_bold.EnableWindow();
		m_italic.EnableWindow();
		}
	}

void MTextTextTab::OnUndo() 
	{
	CRichEditCtrl* text = (CRichEditCtrl*) GetParent()->GetDlgItem(IDC_RICHEDIT);
	text->Undo();
	text->SetFocus();
	}

void MTextTextTab::OnSelendokFontsize() 
	{

	CRichEditCtrl* pText = (CRichEditCtrl*) GetParent()->GetDlgItem(IDC_RICHEDIT);
	pText->SetFocus();

	}

void MTextTextTab::OnSetfocusFontsize() 
	{
	CRichEditCtrl* pText = (CRichEditCtrl*) GetParent()->GetDlgItem(IDC_RICHEDIT);
	pText->GetSel(m_begin, m_end);	
	}

void MTextTextTab::OnKillfocusFontsize() 
	{
	DM_REAL* real_val = NULL;
	CString text;
	m_fontsize.GetWindowText(text);
	char* strval = "";
	strval = text.GetBuffer(0);
	if (!ic_strIsValidReal(strval, false, true, real_val)) 
		{
	//	MessageBox("Unable to use this value for this unit entry.  Please try another value.",
	//					ResourceString(DNT_ICADAPP_DIMINE_1, "DIMINE"),
	//					MB_OK | MB_SETFOREGROUND);
	
		

		m_fontsize.SetWindowText(((MTextPropSheet*) GetParent())->m_fsize);
		m_fontsize.SetFocus();
		return;
		}
    double SizeFromComboBox = atof (text);
	CRichEditCtrl* pText = (CRichEditCtrl*) GetParent()->GetDlgItem(IDC_RICHEDIT);
	CHARFORMAT cf;
	pText->SetSel(0, 1);
	pText->GetSelectionCharFormat(cf);
	CString message;
	pText->SetSel(m_begin, m_end);
	if (m_end - m_begin < pText->GetTextLength()) 
		{
		if (SizeFromComboBox > (cf.yHeight / 125.0 + .0001))
			{
			message.Format( _T ("This value is too large in relationship to the rest of the text.  The largest value you can enter is %.3lf.  You may change the text size without limits by selecting the entire text."), cf.yHeight/125.0);
			
			message.Format(_T ("%.3lf"), cf.yHeight / 125.0);
			m_fontsize.SetWindowText(message);
			m_fontsize.SetFocus();
			return;
			}
		if (SizeFromComboBox < (cf.yHeight / 4000.0 - .0001)) 
			{
			message.Format( _T ("This value is too small in relationship to the rest of the text.  The smallest value you can enter is %.3lf.  You may change the text size without limits by selecting the entire text."), cf.yHeight/4000.0);
		
			message.Format(_T ("%.3lf"), cf.yHeight / 4000.0);
			m_fontsize.SetWindowText(message);
			m_fontsize.SetFocus();
			return;
			}
		}
 	CString string;
	string.Format (_T ("%.3lf"), SizeFromComboBox);
	if (m_fontsize.FindStringExact(-1, string) == CB_ERR)
		m_fontsize.AddString (string);
	UpdateSize();
	}


void MTextTextTab::UpdateSize()
{
	CRichEditCtrl* pText = (CRichEditCtrl*) GetParent()->GetDlgItem(IDC_RICHEDIT);
	CHARFORMAT cf;
	pText->GetSelectionCharFormat(cf);
    cf.dwMask = CFM_SIZE;
	CString string;
	int nIndex = m_fontsize.GetCurSel();
	if (nIndex == CB_ERR)
		m_fontsize.GetWindowText(string);
	else
		m_fontsize.GetLBText(nIndex, string);
	double size = atof(string);
	
	struct DATA_RES_BUF setgetrb;
	setgetrb.restype=RTREAL;
	setgetrb.resval.rreal=size;
	//dm_setvar("TEXTSIZE"/*DNT*/,&setgetrb);  //save the text size tzh 2008-08
	
	//DP: old realization commented
	//cf.yHeight = (int) ((size / 0.2) * 200 + 0.5); // .2 is default fontsize in the combo box.  200 is
	ConvertTextHeightToTwips(size, cf.yHeight);	   // the actual default fontsize, plus .5 to 
	// offset a small rounding error.
	//cf.yHeight=250;
	cf.yHeight=(int) ((size / 0.2) * 200 + 0.5);
	((MTextPropSheet *)GetParent())->m_FontTrueSize=size;
	pText->SetSelectionCharFormat(cf);
}


